using System;
using System.Collections.Generic;
using HashFoo.Rest.Common;

namespace HashFoo.Rest.CommonTests.MockResources
{
    public class ResourceSerializerMock : IResourceSerializer
    {
        public const string DefaultStringResultModel = "mock-string-result-model";

        public ResourceSerializerMock()
        {
            // add a default string deserializer, since we use that a lot in our tests as a result model.
            ModelDeserializerOverrides = new Dictionary<Type, Func<object>>
            {
                {typeof(string), () => DefaultStringResultModel }
            };

            ModelSerializerOverrides = new Dictionary<Type, Func<string>>();
        }

        public IDictionary<Type, Func<object>> ModelDeserializerOverrides { get; private set; }
        public IDictionary<Type, Func<string>> ModelSerializerOverrides { get; private set; }

        public T Read<T>(string data)
        {
            var modelType = typeof(T);
            if (ModelDeserializerOverrides.ContainsKey(modelType)) return (T)ModelDeserializerOverrides[modelType]();

            // else default
            return Activator.CreateInstance<T>();
        }

        public object Read(string data, Type type)
        {
            throw new NotImplementedException();
        }

        public string Write<T>(T data)
        {
            var modelType = typeof(T);
            return ModelSerializerOverrides.ContainsKey(modelType)
                ? ModelSerializerOverrides[modelType]()
                : "mock-serialized-model";
        }

        public string[] AcceptMimeTypes
        {
            get { return new[] { "text/mock" }; }
        }

        public string ContentMimeType
        {
            get { return "text/mock"; }
        }
    }
}